package com.tianshi.agriculture.utils;

public class ToolsUtil {
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replace(" ", "");
        int len = hexString.length();
        byte[] byteArray = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            byteArray[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i + 1), 16));
        }
        return byteArray;
    }

    public static boolean isShortType(String str) {
        try {
            Short.parseShort(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean IsValidShort(String value,int from,int to) {
        if(isShortType(value) == false) {
            return false;
        }

        short shortValue = Short.valueOf(value);
        if(shortValue < from || shortValue > to) {
            return false;
        }

        return true;
    }


    public static boolean isIntType(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean IsValidInt(String value,int from,int to) {
        if(isIntType(value) == false) {
            return false;
        }

        Integer intValue = Integer.valueOf(value);
        if(intValue < from || intValue > to) {
            return false;
        }

        return true;
    }

    public static boolean isFloatType(String str) {
        try {
            Float.parseFloat(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean IsValidFloat(String value,int from,int to) {
        if(isFloatType(value) == false) {
            return false;
        }

        Float intValue = Float.valueOf(value);
        if(intValue < from || intValue > to) {
            return false;
        }

        return true;
    }
}
